//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "PlotPatternsParameters.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHTEXT         10000
#define DIMENSION1DDATA    5
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPatternsParameters::PlotPatternsParameters () {
  
  this->title             = new String ();
  this->filenameOut       = new String ();
  this->patternsList      = new TListE <Pratt2PatternsByCATH> ();
  this->patternsListType  = new TListE <String> ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPatternsParameters::PlotPatternsParameters (const PlotPatternsParameters& plotPatternsParameters) {
  
  this->title             = new String (plotPatternsParameters.title);
  this->filenameOut       = new String (plotPatternsParameters.filenameOut);
  this->patternsList      = new TListE <Pratt2PatternsByCATH> (plotPatternsParameters.patternsList);
  this->patternsListType  = new TListE <String> (plotPatternsParameters.patternsListType);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPatternsParameters::PlotPatternsParameters (const PlotPatternsParameters* plotPatternsParameters) {
  
  this->title             = new String (plotPatternsParameters->title);
  this->filenameOut       = new String (plotPatternsParameters->filenameOut);
  this->patternsList      = new TListE <Pratt2PatternsByCATH> (plotPatternsParameters->patternsList);
  this->patternsListType  = new TListE <String> (plotPatternsParameters->patternsListType);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPatternsParameters::~PlotPatternsParameters () {
  
  if (this->title)            delete this->title;
  if (this->filenameOut)      delete this->filenameOut;
  if (this->patternsList)     delete this->patternsList;
  if (this->patternsListType) delete this->patternsListType;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPatternsParameters::Title (String* title) {
 
  if (title) this->title->In(title);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPatternsParameters::FilenameOut (String* filenameOut) {
 
  if (filenameOut) this->filenameOut->In(filenameOut);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPatternsParameters::PatternsList (TListE <Pratt2PatternsByCATH>* pratt2PatternsByCATH) {
  
  if (pratt2PatternsByCATH) *(this->patternsList) = *pratt2PatternsByCATH;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPatternsParameters::PatternsListType (TListE <String>* patternsListType) {
  
  if (patternsListType) *(this->patternsListType) = *patternsListType;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PlotPatternsParameters::Title (void) {
  
  return this->title;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PlotPatternsParameters::FilenameOut (void) {
  
  return this->filenameOut;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <Pratt2PatternsByCATH>* PlotPatternsParameters::PatternsList (void) {
  
  return this->patternsList;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <String>* PlotPatternsParameters::PatternsListType (void) {
  
  return this->patternsListType;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::PlotProbBelongGroupByContainPattern (void) {
  
  if (!this->patternsList || !this->patternsList->Length() || 
      !this->filenameOut  || !this->filenameOut->Length())
    return false;
  
  gnuplot_ctrl* plot = gnuplot_init();
  char          buffer [LENGTHTEXT + 1];
  
  int           counterPlot = 0, counterRow = 0;
  
  gnuplot_saveplotPNG(plot, this->filenameOut->Out(), 11, 2800, 1400);
  
  sprintf(buffer, "unset key\0");
  gnuplot_cmd(plot, buffer);
  
  sprintf(buffer, "set pointsize 0.8\0");
  gnuplot_cmd(plot, buffer);
  
  sprintf(buffer, "set multiplot layout %ld, 5 title \"%s - Conditional probability (Domain that Bind the Ligand if Contain the Pattern)\" font \"calibri, 18\"\0", this->patternsList->Length(), this->title->Out());
  gnuplot_cmd(plot, buffer);
  
  this->patternsList->SetInitial();
  this->patternsListType->SetInitial();
  while (this->patternsList->SetNext() && this->patternsListType->SetNext()) {
    
    Pratt2PatternsByCATH* currentPratt2PatternsByCATH = this->patternsList->GetCurrent();
    
    counterRow++;
    counterPlot = 0;
    
    currentPratt2PatternsByCATH->ListGroupPrattPatterns()->SetInitial();
    while (currentPratt2PatternsByCATH->ListGroupPrattPatterns()->SetNext()) {

      Pratt2Patterns* currentPratt2Patterns = currentPratt2PatternsByCATH->ListGroupPrattPatterns()->GetCurrent();
      double*         probabilities         = new double [currentPratt2Patterns->Patterns()->Length()]; 
      int             position              = 0;

      counterPlot++;
      
      sprintf(buffer, "unset key\0");
      gnuplot_cmd(plot, buffer);

      sprintf(buffer, "%s for a training set (%d Percentage) \0", this->patternsListType->GetCurrent()->Out(), (int)currentPratt2Patterns->CPercent());
      gnuplot_set_title(plot, buffer);

      gnuplot_set_xrange (plot, 0, currentPratt2Patterns->Patterns()->Length()); 
      gnuplot_set_yrange (plot, (double) 0, (double) 1); 
      
      gnuplot_set_xlabel(plot, (counterRow  == this->patternsList->Length())?"Rank":"", "font \"calibri, 16\"");
      gnuplot_set_ylabel(plot, (counterPlot == 1)?"Probability":"", "font \"calibri, 16\"");

      currentPratt2Patterns->Patterns()->SetInitial();
      while (currentPratt2Patterns->Patterns()->SetNext()) 
        probabilities[position++] = currentPratt2Patterns->Patterns()->GetCurrent()->Probability();

      if (!position) {
        gnuplot_set_xrange (plot, 0, 5000); 
        probabilities = new double [1];
        probabilities[position++] = 0;
      }
      
      gnuplot_plot_x (plot,  probabilities, position, NULL);

      if (probabilities) delete[] probabilities;

    }
    
    if (counterPlot < DIMENSION1DDATA) {
      
      for (int i = counterPlot; i < DIMENSION1DDATA; i++) {

        gnuplot_set_xrange (plot, 0, 5000); 
        gnuplot_set_yrange (plot, (double) 0, (double) 1); 

        double probabilities = 0;

        gnuplot_plot_x (plot,  &probabilities, 1, NULL);  

      }

    }
    
  }
  
  sprintf(buffer, "unset multiplot\0");
  gnuplot_cmd(plot, buffer);
  
  gnuplot_close(plot);
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::PlotNumberSeqContainThePattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::PlotNumberTimesContainThePattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::PlotLengthPattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::HistogramProbBelongGroupByContainPattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::HistogramNumberSeqContainThePattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::HistogramNumberTimesContainThePattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPatternsParameters::HistogramLengthPattern (void) {
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPatternsParameters PlotPatternsParameters::operator= (const PlotPatternsParameters& plotPatternsParameters) {
  
  this->title->In(plotPatternsParameters.title);
  this->filenameOut->In(plotPatternsParameters.filenameOut);
  
  *(this->patternsList)     = *(plotPatternsParameters.patternsList);
  *(this->patternsListType) = *(plotPatternsParameters.patternsListType);
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

